package com.dyh.scdemo.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;

/*
@Author=Joey Wheeler
@Date=2020/10/13 9:54
*/
@RestController
@RequestMapping("hello")
public class HelloController {

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    RestTemplate restTemplate;

    @Value("${myname}")
    public String myname;

    @Value("${name}")
    public String name;

    @GetMapping("hello")
    public String hello(){
        ServiceInstance serviceInstance =
                loadBalancerClient.choose("scdemo-provider");
        if (serviceInstance==null)
            return "error";

//        String uri = String.format(
//                "http://%s:%s/hello/hello",
//                serviceInstance.getHost(),serviceInstance.getPort());

        //String res = restTemplate.patchForObject(uri,"hi",String.class);
        //String res = restTemplate.postForObject(uri,"hi",String.class);
        //String res = restTemplate.getForObject(uri,String.class);
        //return res;
        return "";
    }
}
